//
//  ResetPasswordView.swift
//  GreenDeal
//
//  Created by Moennig Q on 8/12/2022.
//

import UIKit
import RxCocoa
import RxSwift
class ResetPasswordView: MeModelVCBaseView {
    lazy var disposeBag = DisposeBag()
    var oldPwdTextField: TextFieldWithRequiredView!
    var newPwdTextField: TextFieldWithRequiredView!
    var confirmPwdTextField: TextFieldWithRequiredView!
    let saveBtn = OperationButton(R.string.localizable.save())
    var saveClosure: (()->Void)?
    
    override func setUpviews() {
        let contentView = UIView()
        contentView.backgroundColor = .white
        self.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.equalTo(self.topBackView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        //1.old pwd
        let oldPwdTitle = LabelWithRedStarView(R.string.localizable.meChangePWDOldpwd())
        oldPwdTitle.titleLabel.font = R.font.interSemiBold(size: 13)
        contentView.addSubview(oldPwdTitle)
        oldPwdTitle.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.size.greaterThanOrEqualTo(0)
        }
        
        oldPwdTextField = TextFieldWithRequiredView()
        oldPwdTextField.textField.isSecureTextEntry = true
        contentView.addSubview(oldPwdTextField)
        oldPwdTextField.snp.makeConstraints { make in
            make.top.equalTo(oldPwdTitle.snp.bottom).offset(12)
            make.left.equalTo(oldPwdTitle)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //1.new pwd
        let newPwdTitle = LabelWithRedStarView( R.string.localizable.meChangePWDNewpwd())
        newPwdTitle.titleLabel.font = R.font.interSemiBold(size: 13)
        contentView.addSubview(newPwdTitle)
        newPwdTitle.snp.makeConstraints { make in
            make.top.equalTo(oldPwdTextField.snp.bottom).offset(20)
            make.left.equalTo(oldPwdTitle)
            make.size.greaterThanOrEqualTo(0)
        }
        newPwdTextField = TextFieldWithRequiredView(.password)
        newPwdTextField.textField.isSecureTextEntry = true
        contentView.addSubview(newPwdTextField)
        newPwdTextField.snp.makeConstraints { make in
            make.top.equalTo(newPwdTitle.snp.bottom).offset(12)
            make.left.equalTo(oldPwdTitle)
            make.right.equalTo(oldPwdTextField)
            make.height.greaterThanOrEqualTo(0)
            
        }
        //1.confirm pwd
        let confirmPwdTitle = LabelWithRedStarView(R.string.localizable.meChangePWDConfirmpwd())
        confirmPwdTitle.titleLabel.font = R.font.interSemiBold(size: 13)
        contentView.addSubview(confirmPwdTitle)
        confirmPwdTitle.snp.makeConstraints { make in
            make.top.equalTo(newPwdTextField.snp.bottom).offset(20)
            make.left.equalTo(oldPwdTitle)
            make.size.greaterThanOrEqualTo(0)
        }
        confirmPwdTextField = TextFieldWithRequiredView(.password)
        confirmPwdTextField.textField.isSecureTextEntry = true
        contentView.addSubview(confirmPwdTextField)
        confirmPwdTextField.snp.makeConstraints { make in
            make.top.equalTo(confirmPwdTitle.snp.bottom).offset(12)
            make.left.equalTo(oldPwdTitle)
            make.right.equalTo(oldPwdTextField)
            make.height.greaterThanOrEqualTo(0)
        }
        //save btn
        contentView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.left.right.equalTo(oldPwdTextField)
            make.height.equalTo(OperationButton.height)
        }
        saveBtn.addTarget(self, action: #selector(saveBtnClick), for: UIControl.Event.touchUpInside)
        //new
        confirmPwdTextField.editingDidEndClosure = {[weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.checkIfPwdAndConfirmPwdInconsistent()
        }
        newPwdTextField.editingDidEndClosure = {[weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.checkIfPwdAndConfirmPwdInconsistent()
        }
        self.checkEverythingValid()
    }
    //MARK: - 检查密码是否匹配
    private func checkIfPwdAndConfirmPwdInconsistent(){
        let newPwd = (self.newPwdTextField.textField.text ?? "")
        let confirmPwd = (self.confirmPwdTextField.textField.text ?? "")
        if newPwd != confirmPwd && confirmPwd != "" && newPwd != ""{
            self.confirmPwdTextField.showInvalidMsg(R.string.localizable.changePasswordPasswordsInconsistent())
        }
    }
    @objc func saveBtnClick(){
        self.endEditing(true)
        self.saveClosure?()
    }
    //MARK: - 必填项数据绑定控制save btn是否可用
    private func checkEverythingValid(){
        //1.gridConnected
        let oldPwdValid = self.oldPwdTextField.textField.rx.observe(String.self, "text").map({ ($0 ?? "").count > 0 }).share(replay: 1)
        let newPwdValid = self.newPwdTextField.textField.rx.observe(String.self, "text").map({ Tool().isValidPwd(pwd: $0 ?? "") }).share(replay: 1)
        let confirmPwdValidAndConsistent = self.confirmPwdTextField.textField.rx.observe(String.self, "text").map({
            let newPwd = (self.newPwdTextField.textField.text ?? "")
            let confirmPwd = (self.confirmPwdTextField.textField.text ?? "")
            if Tool().isValidPwd(pwd: $0 ?? "") && confirmPwd == newPwd{
                return true
            }
            return false
        }).share(replay: 1)
        let everythingValid = Observable.combineLatest(
            oldPwdValid,
            newPwdValid,
            confirmPwdValidAndConsistent
        ) { $0 && $1 && $2 }
            .share(replay: 1)
        //
        everythingValid.subscribe(onNext: {[weak self] (valid) in
            guard let strongSelf = self else {return}
            if valid {
                strongSelf.saveBtn.enableBtn()
            }else{
                strongSelf.saveBtn.disableBtn()
            }
        }).disposed(by: self.disposeBag)
    }
}
